package cn.zlj.pethome.org.controller;
import cn.zlj.pethome.basic.QueryResult;
import cn.zlj.pethome.basic.domain.Logininfo;
import cn.zlj.pethome.basic.util.AjaxResult;
import cn.zlj.pethome.basic.util.LoginContext;
import cn.zlj.pethome.org.domain.TEmployee;
import cn.zlj.pethome.org.query.QueryPage;
import cn.zlj.pethome.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class TEmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PutMapping
    public AjaxResult save(@RequestBody TEmployee t){
        if(t.getId()==null){
            employeeService.save(t);
        }else {
            employeeService.update(t);
        }
        return  AjaxResult.me().success();
    }
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        if(id==null){
        throw new RuntimeException();
        }
       employeeService.del(id);
        return  AjaxResult.me().success();
    }
    @GetMapping
    public List<TEmployee> list(){
       return employeeService.getList();
    }
    @GetMapping("/{id}")
    public TEmployee getOne(@PathVariable("id") Long id){
        return employeeService.getOne(id);
    }
    @PostMapping
    public QueryResult<TEmployee> queryResult(@RequestBody QueryPage page){
        return employeeService.queryPage(page);
    }

    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        employeeService.batchDel(ids);
        return AjaxResult.me().success();
    }
//selectByShopId
@GetMapping("/selectByShopId")
public List<TEmployee> selectByShopId(HttpServletRequest request){
    Logininfo logininfo = LoginContext.getLogininfo(request);
    TEmployee tEmployee = employeeService.selectByLogininfoId(logininfo.getId());
    return employeeService.selectByShopId(tEmployee.getShopId());
}
}
